﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp._console.core
{
    public partial class application_module_assign : System.Web.UI.Page
    {
        private Guid _applicationId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                _applicationId = new Guid(Request.QueryString["id"]);
            }

            if (!IsPostBack)
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();

                if (_applicationId != Guid.Empty)
                {
                    SmartSpace.Core.Site application = service.GetSite(_applicationId);

                    _ctlLabelName.Text = application.Title;
                    _ctlLabelType.Text = application.Type;

                    List<Module> assignedModules = service.GetModules(_applicationId);

                    List<Module> allModules = service.GetModules(application.Type, 0, 999);

                    _ctlListAssigned.DataSource = assignedModules;
                    _ctlListAssigned.DataBind();
                    List<Module> unassignedModules = new List<Module>();
                    for (int i = 0; i < allModules.Count; i++)
                    {
                        bool isExist = false;
                        foreach (var item in assignedModules)
                        {
                            if (item.Id == allModules[i].Id)
                            {
                                isExist = true;
                                break;
                            }
                        }
                        if (!isExist)
                        {
                            unassignedModules.Add(allModules[i]);
                        }
                    }
                    _ctlListUnassigned.DataSource = unassignedModules;
                    _ctlListUnassigned.DataBind();
                }
            }
            _ctlLinkBack.NavigateUrl = "./application-list.aspx";
        }

        protected void _ctlBtnDisable_Click(object sender, EventArgs e)
        {
            LinkButton btn = sender as LinkButton;
            ICoreService service = ServiceLocator.Get<ICoreService>();

            Guid moduleId = new Guid(btn.CommandArgument);

            service.RemoveModuleFromSite(_applicationId, moduleId);

            Response.Redirect(Request.Url.AbsoluteUri);
        }

        protected void _ctlBtnEnable_Click(object sender, EventArgs e)
        {
            LinkButton btn = sender as LinkButton;
            ICoreService service = ServiceLocator.Get<ICoreService>();

            Guid moduleId = new Guid(btn.CommandArgument);

            service.AssignModuleToSite(_applicationId, moduleId);

            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }
}